# role.py
from agent import Agent
from debate_recorder import DebateRecorder
from llm_integration import LLMIntegration
from debate_agents.base import BaseDebateAgent

class Role(Agent):
    def __init__(self, name, recorder, llm=None):
        super().__init__(name, recorder)
        self.llm = llm

    def interact_with_llm(self, prompt):
        # 这里可调用 LLM 集成模块的方法
        # 为了简化，暂时返回一个占位信息
        if self.llm:
            return self.llm.invoke(prompt).content
        else:
            return "LLM 未初始化"


class Host(Role):
    def __init__(self, name, recorder: DebateRecorder):
        super().__init__(name, recorder)
        self.current_stage = None

    def speak(self, message):
        self.recoder.record(stage=self.current_stage, speaker=self.name, content=message)
        return super().speak(message)

    def start_debate(self, topic):
        """开始辩论赛"""
        self.current_stage = "开始阶段"
        message = f"欢迎同学们参加辩论赛! 今天的辩论题目是{topic},现在辩论开始!"
        return self.speak(message)

    def control_stage(self, stage):
        """控制辩论赛的阶段转换"""
        self.current_stage = stage
        return self.speak(f"现在我们进入辩论赛的{stage}.\n"
                          f" 请双方辩手认真倾听对方观点,每次发言必须有实质性内容,使用具体的数据和案例支持论点.")

    def statr_judge(self):
        return self.speak("所有辩论环节到此结束，现在请裁判员对本场辩论进行点评，并宣布比赛结果!")

    def end_debate(self):
        """结束辩论赛"""
        return self.speak("辩论赛到此结束，感谢参加裁判员，双方队员和幕前幕后的所有工作人员!")


class Judge(Role):
    def __init__(self, name, recorder: DebateRecorder, llm: LLMIntegration = None):
        super().__init__(name, recorder, llm)

    def speak(self, message):
        self.recoder.record(stage="裁判总结和宣判结果", speaker=self.name, content=message)
        return super().speak(message)

    def summarize(self):
        """总结辩论内容"""
        prompt = f"""你是一个中文辩论赛的裁判，现在需要做最终总结。请用大白话总结双方表现，主要看哪边的论点更有说服力、证据更扎实，
            总结控制在1000字以内。必须明确判定胜负：
            1. 如果一方占优，直接宣布获胜方（正方或者反方）
            2. 如果实在难分高下，可以说『双方旗鼓相当，判定平局』
            现在请基于以下辩论记录做总结：{self.recoder.summarize_records()}，并宣布最终获胜方。"""

        summary = self.interact_with_llm(prompt)
        self.recoder.record(stage="裁判总结和宣判结果", speaker=self.name, content=summary)
        return self.speak(summary)


class Debater(Role):
    def __init__(self, name, side, recorder: DebateRecorder, topic, prompt="", position="", is_person=False,
                 llm: BaseDebateAgent = None):
        super().__init__(name, recorder, llm)
        self.side = side
        self.position = position
        self.topic = topic
        self.prompt = prompt
        self.is_person = is_person

    def speak(self, message):
        content = f"{self.side}{self.name}:\n {message}"
        print(content)
        return content

    def debate(self, stage, action_type):

        message = self.llm.invoke(side=self.side,
                                  topic=self.topic,
                                  stage=stage,
                                  role=self.position,
                                  role_prompt=self.prompt,
                                  task=action_type,
                                  history=self.recoder.summarize_records())
        self.recoder.record(stage=stage, speaker=self.name, content=message, side=self.side)
        return self.speak(message)


class HumanDebater(Agent):
    def __init__(self, name, side, recorder: DebateRecorder, position=""):
        super().__init__(name, recorder)
        self.side = side
        self.action_type = ""
        self.position = position
        self.stage = ""

    def add_record(self, message):
        self.recoder.record(stage=self.stage, speaker=self.name, content=message, side=self.side)
